package itsm.isperp.module.service.incident;

import itsm.isperp.module.entity.incident.IcdIncident;
import itsm.isperp.module.entity.incident.IcdMajorHandle;
import itsm.isperp.module.repository.incident.IcdIncidentMapper;
import itsm.isperp.module.service.app.AppUserService;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * 重要事件管理服务
 * 
 * @author lizx
 * @date 2013-4-11
 * @version V1.0
 */
@Service
public class IcdIncidentMajorService {

	public static final Logger logger = LoggerFactory
			.getLogger(IcdIncidentMajorService.class);

	@Autowired
	protected AppUserService appUserService;
	
	@Autowired
	protected IcdIncidentMapper icdIncidentMapper;
	
	@Autowired
	protected JdbcTemplate jdbcTemplate;

	@Autowired
	protected IcdMajorHandleService icdMajorHandleService;

	// 两小时内报单数
	public int get2HourTotal(String categoryId) {

		String sql = "SELECT COUNT(*)  FROM icd_incident WHERE status IN ('一线分派','二线受理','二线处理','重新分派','挂起审核','挂起处理')"
				+ " AND create_date > SYSDATE - 2/24   AND  create_date <SYSDATE"
				+ " AND ICD_CATEGORY_ID='" + categoryId + "'";
		return jdbcTemplate.queryForObject(sql, Integer.class);

	}

	// 超过2小时没有解决的
	public int getOver2HourSolveTotal(String categoryId) {

		String sql = "SELECT COUNT(*)  FROM  icd_incident where status IN ('一线分派','二线受理','二线处理','重新分派','挂起审核','挂起处理') "
				+ " AND incident_type IN('故障','告警')"
				+ " AND create_date < SYSDATE - 2/24   AND    ICD_CATEGORY_ID='"
				+ categoryId + "'";
		return jdbcTemplate.queryForObject(sql, Integer.class);

	}

	// sla超期数
	public int getOverSlaTotal(String categoryId) {

		String sql = "SELECT COUNT(*)  FROM  icd_incident where status IN ('一线分派','二线受理','二线处理','重新分派','挂起审核','挂起处理')"
				+ " AND  sla_status=1  AND  ICD_CATEGORY_ID='"
				+ categoryId
				+ "'";
		return jdbcTemplate.queryForObject(sql, Integer.class);

	}

	/**
	 * 初始化重要事件的成员
	 * 
	 * @param incidentId 事件单id
	 */
	public void initMember(String incidentId) {
		IcdIncident incident = icdIncidentMapper.findOne(incidentId);
		String usersCondition = "";
		IcdMajorHandle handle = new IcdMajorHandle();
		handle.setHandler("admin");
		handle.setHandlerName("admin");
		handle.setHandleTime(new Timestamp(new Date().getTime()));
		handle.setIncidentId(incidentId);
		handle.setContents("建立应急小组");
		icdMajorHandleService.saveOrUpdate(handle);

		// this.jdbcTemplate.update("UPDATE icd_major_member SET order_num=1 WHERE ");

	}

	/**
	 * 获取重大事件总数
	 * 
	 * @return 总数
	 */
	public String getMajorTotal() {
		String sql = "SELECT COUNT(*)  FROM  icd_incident WHERE are_major=1 AND status<>'已关闭'";
		return jdbcTemplate.queryForObject(sql, String.class);
	}

}
